package com.dingwen.treasure.kettle.execute;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.dingwen.treasure.kettle.constant.KettleConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 　TransExecute
 * 　@author dingwen
 * 　@date 2022/8/21
 */
@Component
@Async("asyncExecutor")
@Slf4j
public class TransExecute extends AbstractKettleExecute {


    /**
     * 构建器请求参数
     *
     * @param fullFileName 完整文件名称
     * @return {@link Map}<{@link String}, {@link Object}>
     */
    @Override
    protected Map<String, Object> builderRequestParams(String fullFileName) {
        requestParams.put(KettleConstant.KETTLE_CARTE_TRANS, fullFileName);
        requestParams.remove(KettleConstant.KETTLE_CARTE_JOB);
        return requestParams;
    }

    /**
     * 运行
     *
     * @param fullFileName  完整文件名称
     * @param requestParams 请求参数
     */
    @Override
    protected void run(String fullFileName, Map<String, Object> requestParams) {
        String url = buildRequestUrl(requestUrl.concat(KettleConstant.KETTLE_CARTE_REST_TRANS),requestParams);
        HttpResponse response = HttpRequest.get(url)
                .setFollowRedirects(true)
                .header(KettleConstant.KETTLE_CARTE_AUTHORIZATION, requestParams.get(KettleConstant.KETTLE_CARTE_AUTHORIZATION).toString())
                .timeout(3000)
                .body(JSONUtil.toJsonStr(requestParams))
                .execute();
        log.info("请求参数：{}", JSONUtil.toJsonPrettyStr(requestParams));
        log.info("请求地址：{}", url);
        log.info("执行结果：{}", response.getStatus());
    }
}
